package com.itheima.web.controller.sms;


import com.itheima.service.sms.IFlashPromotionSessionService;
import com.itheima.utils.ResultData;
import com.itheima.vo.sms.FlashPromotionSession;
import com.itheima.vo.sms.FlashPromotionSessionVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 限时购场次表 前端控制器
 */
@RestController
@RequestMapping("/flashSession")
@Slf4j
public class FlashPromotionSessionController {
    @DubboReference
    private IFlashPromotionSessionService flashPromotionSessionService;
    @PostMapping("/create")
    public ResultData save(@RequestBody FlashPromotionSession flashPromotionSession){
        flashPromotionSession.setCreateTime(LocalDateTime.now());
        flashPromotionSession.setStartTime(flashPromotionSession.getStartTime().plusHours(8));
        flashPromotionSession.setEndTime(flashPromotionSession.getEndTime().plusHours(8));
        flashPromotionSessionService.save(flashPromotionSession);
        return ResultData.ok("","");
    }
    @PostMapping("/delete/{id}")
    public ResultData delete(@PathVariable Long id){
        flashPromotionSessionService.removeById(id);
        return ResultData.ok("","");
    }
    @GetMapping("/list")
    public ResultData list(){
        List<FlashPromotionSession> list = flashPromotionSessionService.list();
        return ResultData.ok("",list);
    }

    /**
     * 获取全部可选场次及其数量
     * @param flashPromotionId
     * @return
     */
    @GetMapping("/selectList")
    public ResultData selectList(@RequestParam Long flashPromotionId){
        List<FlashPromotionSessionVo> list = flashPromotionSessionService.selectList(flashPromotionId);
            return  ResultData.ok("",list);
    }
    @PostMapping("/update/status/{id}")
    public ResultData updateStatus(@PathVariable Long id,@RequestParam Integer status ){
        FlashPromotionSession flashPromotionSession=new FlashPromotionSession();
        flashPromotionSession.setId(id);
        flashPromotionSession.setStatus(status);
        flashPromotionSessionService.updateById(flashPromotionSession);
        return ResultData.ok("","");
    }
    @PostMapping("/update/{id}")
    public ResultData update(@PathVariable Long id,@RequestBody FlashPromotionSession flashPromotionSession ){
        flashPromotionSession.setId(id);
        flashPromotionSession.setStartTime(flashPromotionSession.getStartTime().plusHours(8));
        flashPromotionSession.setEndTime(flashPromotionSession.getEndTime().plusHours(8));
        flashPromotionSessionService.updateById(flashPromotionSession);
        return ResultData.ok("","");
    }
    @GetMapping("/{id}")
    public ResultData getById(@PathVariable Long id){
        FlashPromotionSession byId = flashPromotionSessionService.getById(id);
        return ResultData.ok("",byId);
    }

}

